﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using System.IO;
using System.Text;

namespace MJCOMMEN_CS.Commen_cs
{
    public class Web_Request
    {
        private string _exceptionStr = "";
        private byte[] ExceptionData;
        private bool IsDisposed;
        private int StatusCodeIT = -1;
        public HttpWebRequest WebRequestIT;
        public HttpWebResponse WebResponseIT;

        public Web_Request(string WebUrl, string RequestMethod)
        {
            this.WebRequestIT = (HttpWebRequest)WebRequest.Create(WebUrl);
            this.WebRequestIT.Method = RequestMethod;
            this.WebRequestIT.Timeout = 5 * 60 * 1000;
        }
        /// <summary>
        /// 获取get参数
        /// </summary>
        /// <param name="par">需要获取的参数名</param>
        /// <returns></returns>
        public static string QueryString(string par)
        {
            string v = "";
            if (HttpContext.Current.Request.QueryString[par] != null)
            {
                v = HttpContext.Current.Request.QueryString[par];
            }
            return v;
        }

        public void Dispose()
        {
            if (!this.IsDisposed)
            {
                try
                {
                    if (this.WebResponseIT != null)
                    {
                        this.WebResponseIT.Close();
                        this.WebResponseIT = null;
                    }
                    if (this.WebRequestIT != null)
                    {
                        this.WebRequestIT.Abort();
                        this.WebRequestIT = null;
                    }
                }
                catch
                {
                }
                finally
                {
                    this.IsDisposed = true;
                }
            }
        }

        ~Web_Request()
        {
            this.Dispose();
        }

        public int GetStatusCode()
        {
            if ((this.StatusCodeIT == -1) && (this.WebResponseIT != null))
            {
                return (int)this.WebResponseIT.StatusCode;
            }
            return this.StatusCodeIT;
        }

        public bool SendPostData(byte[] PostData)
        {
            if (PostData == null)
            {

                return false;
            }

            int count = 0x19000;

            this.WebRequestIT.ContentType = "application/x-www-form-urlencoded";

            this.WebRequestIT.ContentLength = PostData.Length;
            Stream requestStream = this.WebRequestIT.GetRequestStream();
            if (PostData.Length <= count)
            {
                requestStream.Write(PostData, 0, PostData.Length);
            }
            else
            {
                int offset = 0;
                while (offset < PostData.Length)
                {
                    if ((PostData.Length - offset) >= count)
                    {
                        requestStream.Write(PostData, offset, count);
                        offset += count;
                    }
                    else
                    {
                        requestStream.Write(PostData, offset, PostData.Length - offset);
                        break;
                    }
                }
            }
            requestStream.Close();
            return true;

            //  return false;
        }

        public bool SendPostText(string PostString)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(PostString);
            return this.SendPostData(bytes);
        }


        /// <summary>
        /// 获取对方响应回来的数据
        /// </summary>
        /// <returns>返回以字符串的形式</returns>
        public string GetResponseText()
        {
            string result = "";
            this.WebResponseIT = (HttpWebResponse)this.WebRequestIT.GetResponse();
            this.StatusCodeIT = (int)this.WebResponseIT.StatusCode;
            Stream responseStream = this.WebResponseIT.GetResponseStream();
            using (System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            return result;
        }
        /// <summary>
        /// 获取对方响应回来的数据
        /// </summary>
        /// <returns>返回以字节的形式，比如是返回图片之类不是字符串的内容</returns>
        public byte[] GetResponseData()
        {
            byte[] buffer = null;
            try
            {
                int count = 0;
                byte[] buffer2 = new byte[0x400];
                this.WebResponseIT = (HttpWebResponse)this.WebRequestIT.GetResponse();
                this.StatusCodeIT = (int)this.WebResponseIT.StatusCode;
                Stream responseStream = this.WebResponseIT.GetResponseStream();
                MemoryStream stream2 = new MemoryStream();
                using (System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.UTF8))
                {
                     reader.ReadToEnd();
                }
                buffer = stream2.ToArray();
                stream2.Close();
                responseStream.Close();
            }
            catch(Exception ex)
            {

            }
            return buffer;
        }
    }
}